using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public static Game1 instance;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont spriteFont;
        Timer timer = new Timer();

        public Random rnd = new Random();
        public float RndFloat()
        {
            float inv = 1.0f / 65535.0f;
            int r = rnd.Next(65535);
            return ((float)r) * inv;
        }

        public Plane plane;
        public AlienManager alienManager = new AlienManager();
        public BulletManager bulletManager = new BulletManager();
        public BulletManager alienBulletManager = new BulletManager();
        public CollisionManager collisionManager = new CollisionManager();
        public ExplosionManager explosionManager = new ExplosionManager();
        public static Texture2D whiteTexture;
        //public SoundEffect soundExplosion;
        public static int width = 800;
        public static int height = 1080;
        public int lifeCount = 99999;
        public int score = 0;

        public Game1()
        {
            instance = this;
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            whiteTexture = Content.Load<Texture2D>("DefaultWhite");
            spriteFont = Content.Load<SpriteFont>("spriteFont1");
//             Song music1 = Content.Load<Song>("Music1");
//             MediaPlayer.Play(music1);

            //soundExplosion = Content.Load<SoundEffect>("Explosion");         

            plane = new Plane();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            timer.Update();

            // TODO: Add your update logic here
            if (lifeCount > 0)
                plane.Update(timer);

            alienManager.Update(timer);           
            bulletManager.Update(timer);
            alienBulletManager.Update(timer);
            collisionManager.Update(bulletManager, alienManager);

            if (lifeCount > 0)
                collisionManager.UpdatePlayerCollision(alienBulletManager, alienManager, plane);

            explosionManager.Update(timer);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            Rectangle rect = new Rectangle(256,256,4,4);

            if (lifeCount > 0)
                plane.Draw(spriteBatch);

            alienManager.Draw(spriteBatch);
            bulletManager.Draw(spriteBatch);
            alienBulletManager.Draw(spriteBatch);
            explosionManager.Draw(spriteBatch);

            string txt = "LIVE " + lifeCount.ToString();
            spriteBatch.DrawString(spriteFont, txt, new Vector2(16, 16), Color.White);

            txt = "SCORE " + score.ToString();
            spriteBatch.DrawString(spriteFont, txt, new Vector2(width - 200, 16), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }

        public bool IsOutOfScreen(Rectangle rc)
        {
            Rectangle bounds = graphics.GraphicsDevice.Viewport.Bounds;
            
            if (rc.Left > bounds.Right)
                return true;
            if (rc.Right < bounds.Left)
                return true;
            if (rc.Bottom < bounds.Top)
                return true;
            if (rc.Top > bounds.Bottom)
                return true;
            
            return false;
        }
    }
}
